<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="评价详情"></uni-navbar></view>

		<view class="detail_con">
			<view class="con_yt_detail">
				<view class="a_con_status">
					<image class="status_img" :src="$imagePreview(issueInfo.touxiang)" mode="aspectFill"></image>
					<text class="status_name">{{ issueInfo.createByName }}</text>
				</view>
			</view>

			<view class="con_title">
				<view class="t_tit">{{ $htmlRegx(issueInfo.content) }}</view>

				<block v-if="filelist.length > 0">
					<view class="list_img" v-for="(item2, index2) in filelist" :key="index2">
						<image class="image" :src="baseUrl + '/file/download/preview/0?path=' + item2.path" mode="aspectFill"></image>
					</view>
				</block>
			</view>

			<view class="con_status" style="color: #666">
				<text class="a_tlz_time">{{ issueInfo.createTime | parseTime('{y}-{m}-{d}') }}</text>
				<text class="s_length">共{{ issueInfo.readNum }}个评论</text>
			</view>
		</view>

		<view class="con_yt_detail">
			<view class="a_con_item" v-for="(item, index) in issueList" :key="index">
				<view class="a_con_status">
					<image class="status_img" :src="$imagePreview(item.touxiang)" mode="aspectFill"></image>
					<text class="status_name">{{ item.createByName }}</text>
					<!-- <text class="status_lb">{{ item.jiebie }}</text> -->
					<!-- <view class="dz_img" @click="toAddLike(item)"><u-icon name="thumb-up" :color="item.isLike == 0 ? '#999' : '#f00'" size="32"></u-icon></view>
					<text class="status_dz_num" :style="'color:' + (item.isLike == 0 ? '#999' : '#f00')">{{ item.likeNum }}</text> -->
				</view>
				<view class="a_con_content">
					<u-read-more ref="uReadMore" :key="index" text-indent="0" close-text="展开"><u-parse :html="$htmlRegx(item.content)" @load="parseLoaded"></u-parse></u-read-more>
				</view>
				<view class="a_tlz_p">
					{{ item.createTime }}
					<text v-if="issueInfo.nodeStatus == 3" style="text-indent: 20rpx; display: inline-block; color: #333" @click="huifu(item)">回复</text>
				</view>

				<list v-if="item.childList != null" :issueList="item.childList" :flaga="issueInfo.nodeStatus" @pChangeType="huifu"></list>
			</view>

			<u-loadmore :status="loading" />
		</view>

		<u-form-item class="form-item" left-icon="edit-pen" v-if="issueInfo.nodeStatus == 3">
			<u-input v-model="content" type="textarea" input-align="left" placeholder="写评论..." :clearable="false" :focus="isFocus" :height="42" :maxlength="-1" />
			<u-button slot="right" type="error" size="medium" @click="submit">发表</u-button>
		</u-form-item>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import { mapState } from 'vuex';
import list from '@/components/list_lt.vue';
import * as config from '@/config';
export default {
	components: {
		list
	},
	data() {
		return {
			baseUrl: config.def().baseUrl,
			isFocus: false,
			discussId: '',
			discussPath: '',
			scrollTop: 0,
			id: '',
			flaga: [],
			pageNum: 1,
			pageSize: 10,
			clientCode: '',
			issueInfo: {},
			filelist: [],
			issueList: [],
			content: '',
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		this.id = option.id;
		this.loadData();
	},
	onPullDownRefresh() {
		this.loadLogin();
		this.loadData();
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadList('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.currentUserId = this.userInfo.sysUser.id;
			} else {
				this.$store.commit('logout');
				this.$msg('请先登录');
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}, 500);
			}
		},
		loadData() {
			this.$request('/sqmybbs/query/' + this.id, '', {
				load: true
			}).then((res) => {
				this.issueInfo = res.data.sqmyBbs;
				this.filelist = res.data.fileList;
			});
			this.loadList('refresh');
		},
		loadList(type = 'add') {
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}

			let _this = this;

			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				clientCode: this.clientCode,
				clueId: this.id,
				currentUserId: this.currentUserId
				//wlyzIssueContent: {}
			};
			this.$request('/sqmybbs/content/list', data).then((res) => {
				let count = 0;
				if (type != 'add') {
					this.issueList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.issueList = this.issueList.concat(res.data.records);
				if (this.$refs.uReadMore) {
					this.$nextTick(() => {
						if (this.$refs.uReadMore) {
							this.$refs.uReadMore.forEach((item) => {
								item.init();
							});
						}
					});
				}
				this.loaded = true;
				if (this.issueList.length <= 0) {
					this.loading = 'nodata';
				}
				if (type != 'add') {
					uni.stopPullDownRefresh();
				}
			});
		},
		toAddLike(item) {
			let like = '1';
			if (item.isLike == 1) {
				like = '0';
			}
			let data = {
				wlyzIssueContentLike: {
					clientCode: item.clientCode,
					contentId: item.id,
					createBy: item.createBy,
					isLike: like
				}
			};
			this.$request('/wlyz/issue/content/like/add', data).then((res) => {
				this.$msg(res.msg);
				if (like == '0') {
					item.isLike = 0;
					item.likeNum--;
				} else {
					item.isLike = 1;
					item.likeNum++;
				}
			});
		},
		parseLoaded() {
			// this.$refs.uReadMore.init();
		},
		huifu(t) {
			this.content = '回复：';
			this.isFocus = true;

			this.discussId = t.id;
			if (t.discussPath) {
				this.discussPath = t.id + ',' + t.discussPath;
			} else {
				this.discussPath = t.id;
			}
		},
		submit() {
			if (!this.content) {
				this.$msg('提交内容不能为空！');
				return false;
			}

			let data = {
				sqmyBbsContent: {
					clientCode: this.clientCode,
					clueId: this.id,
					createBy: this.currentUserId,
					content: this.content,
					discussType: '0'
				}
			};

			if (this.content.indexOf('回复：') != -1 && this.discussId != '') {
				this.content = this.content.replace('回复：', '');
				data = {
					sqmyBbsContent: {
						clientCode: this.clientCode,
						clueId: this.id,

						discussId: this.discussId,
						discussPath: this.discussPath,

						createBy: this.currentUserId,
						content: this.content,
						discussType: '1'
					}
				};
			}

			this.$request('/sqmybbs/content/add', data, {
				load: true
			}).then((res) => {
				this.content = '';
				this.discussId = '';
				this.issueInfo.contentCount++;
				this.$msg(res.msg);
				this.loadList('refresh');
			});
		}
	}
};
</script>

<style lang="scss">
.u-back-top {
	bottom: 140rpx !important;
}

.list_img {
	width: 700rpx;
	margin: 0 5rpx;
	text-align: center;

	.image {
		width: 80%;
		height: 300rpx;
		margin: 10rpx auto;
	}
}

.detail-view {
	margin-bottom: 94rpx;
	background-color: #fff;
}

.detail_con {
	padding: 0 30rpx;
	color: #333;
	border-bottom: 4rpx solid #f1f2f3;

	.con_title {
		margin: 36rpx 0;
		width: 100%;
		font-size: 18px;
	}

	.con_status {
		display: flex;
		align-items: center;
		padding-bottom: 20rpx;
		font-size: 13px;

		.s_length {
			margin-left: auto;
			font-size: 14px;
			color: #666;
			line-height: 1.5;
		}
	}
}

.con_yt_detail {
	font-size: 14px;
	color: #333;
	line-height: 1.2;

	.a_con_item {
		padding: 14rpx 30rpx 26rpx;
		border-bottom: 1rpx solid #f1f2f3;
	}

	.a_con_status {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		font-size: 14px;
		color: #999;
		line-height: 50rpx;

		.status_img {
			width: 50rpx;
			height: 50rpx;
			border-radius: 50%;
			overflow: hidden;
			background-color: #f1f2f3;
		}

		.status_name {
			margin: 0 12rpx;
		}

		.status_lb {
			// margin-left: 20rpx;
		}

		.status_dz_num {
			// margin-left: auto;
		}

		.dz_img {
			margin-left: auto;
			width: 36rpx;
		}
	}

	.a_con_content {
		margin-bottom: 12rpx;
		font-size: 16px;
		line-height: 1.4;
		white-space: pre-line;
	}

	.a_tlz_p {
		font-size: 13px;
		color: #999;
	}
}

.form-item {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	z-index: 100;
	padding: 10rpx 30rpx;
	border-top: 4rpx solid #f1f2f3;
	background-color: #fff;

	/deep/ .u-form-item--left__content {
		padding-right: 0;
	}
}

.status_node {
	display: inline-block;
	padding: 8rpx 20rpx;
	font-size: 13px;
	line-height: 1;
}

.st_end {
	color: #999;
	border: 1rpx solid #999;
	border-radius: 10rpx;
}

.st_tlz {
	color: #e5231a;
	border: 1rpx solid #e5231a;
	border-radius: 10rpx;
}

.st_gly {
	margin-left: 10rpx;
	color: #e5231a;
	border: 1rpx solid #e5231a;
	border-radius: 10rpx;
}
</style>
